// import Vue from "vue";
// 引入请求的路径
// import { setToken } from "@/utils/auth";
// import zhrequest from "@/api/axios";
// import { webServiceUrl } from "@/api/baseURL";
// import getfarmreq from "@/api/basic-info-manage/agricultural-type-manage/index";
import Layout from "@/layout";
//import userService from "@/api/user/index"
// import { mymixin } from '@/utils/mixins'
// import agriculturalType from "@/api/basic-info-manage/agricultural-type-manage/index"
import {
  asyncRoutes,
  constantRoutes
} from "@/router";
import {
  getToken
} from "@/utils/auth";

const _import = require("../../router/_import_" + process.env.NODE_ENV); // 获取组件的方法
function filterAsyncRouter(asyncRouterMap) {
  // 遍历后台传来的路由字符串，转换为组件对象
  const accessedRouters = asyncRouterMap.filter((route) => {
    if (route.component) {
      if (route.component === "Layout") {
        // Layout组件特殊处理
        route.component = Layout;
      } else {
        // console.log(route.component)
        if (
          typeof route.component === 'string' ||
          typeof route.component === 'number' ||
          // $flow-disable-line
          typeof route.component === 'symbol' ||
          typeof route.component === 'boolean') {
          if (JSON.stringify(route.component) !== undefined && JSON.stringify(route.component).indexOf("{") !== -1) {
            // console.log(route.component)
            route.component = { render(c) { return c('router-view') } };
          } else {
            if (route.meta?.title === undefined) {
              delete route.meta
            }
            route.component = _import(route.component);
          }
        }
      }
    }
    // if (route.routeId) {
    //   delete route.routeId
    // }
    // if (route.parentId) {
    //   delete route.parentId
    // }
    // if (route.meta.metaId) {
    //   delete route.meta.metaId
    // }
    // if (route.meta.routeId) {
    //   delete route.meta.routeId
    // }
    // if (route.children && route.children.length > 0) {
    //   route.children = filterAsyncRouter(route.children);
    // }
    if (route.children) {
      route.children = filterAsyncRouter(route.children);
    }
    return true;
  });

  return accessedRouters;
}

// function componentuser (url) {
//   import(url)
// }
const state = {
  routes: [],
  addRoutes: [],
  pageElementList: [],
  _axiosPromiseArr: [],
  // 养殖类型
  liveStockType: '',
  liveStockTypeList: ''
};
const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes;
    state.routes = constantRoutes.concat(routes);
  },
  SET_PAGE_ELEMENT: (state, payload) => {
    state.pageElementList = payload;
  },
  SET_STOCK_TYPE: (state, payload) => {
    state.liveStockType = payload;
  },
  SET_STOCK_TYPE_LIST: (state, payload) => {
    state.liveStockTypeList = payload;
  },
  add_AxiosPromiseArr(state, item) {
    // console.log("添加_axiosPromiseArr");
    state._axiosPromiseArr.push(item)
  },
  clear_AxiosPromiseArr(state) {
    // console.log("_axiosPromiseArr情空");
    state._axiosPromiseArr = []
  }
};
// 是否动态权限
const judgePermissiontype = false; // false true
const getters = {
  // 按钮权限管理
  judgePermission: state => {
    return (pageNo, code) => {
      // console.log(pageNo, code)
      // eslint-disable-next-line eqeqeq
      let index = state.pageElementList.findIndex(el => el.pageNo == pageNo && el.code == code);
      if (!judgePermissiontype) {
        return true;
      }
      if (index > -1) {
        return true;
      } else {
        // return true;
        return false;
      }
    }
  },
  livestockPermission: state => {
    return (fieldNo) => {
      let liveStockTypeList = state.liveStockTypeList
      let liveStockType = state.liveStockType || getToken('breedTypeName')
      if (fieldNo !== 'StockType') {
        if (liveStockTypeList && liveStockTypeList.length > 0) {
          let index = liveStockTypeList.findIndex(el => el.fieldNo === fieldNo);
          if (index > -1) {
            return liveStockTypeList[index].fieldName;
          } else {
            return '';
          }
        } else {
          return ''
        }
      } else {
        if (liveStockType) {
          return liveStockType;
        } else {
          return '';
        }
      }
    }
  }
}
// 是否启用动态路由 true false
const nowmenutype = false;
const actions = {
  // generateRoutes({ commit }) {
  //   // eslint-disable-next-line no-async-promise-executor
  //   return new Promise(async (resolve) => {
  //     commit("SET_ROUTES", []);
  //     let accessedRoutes;

  //     let virtualSystemNo = getToken("virtualSystemNo") || 'ZY';
  //     let breedTypeNo = getToken("breedTypeNo");
  //     // 测试
  //     let pagefield = await userService.queryfieldTemplate(breedTypeNo);
  //     commit("SET_STOCK_TYPE_LIST", pagefield.data);
  //     if (nowmenutype) {
  //       let res = await userService.queryMenuAll(virtualSystemNo)
  //       let testjk = [];
  //       let zhrouter = res.data || [];
  //       testjk.push(...zhrouter);
  //       let router404 = {
  //         path: "*",
  //         redirect: "/404",
  //         hidden: true
  //       };
  //       testjk.push(router404);
  //       accessedRoutes = filterAsyncRouter(testjk);
  //       commit("SET_ROUTES", accessedRoutes);

  //       let pageElementRes = await userService.queryPageElement(virtualSystemNo);
  //       commit("SET_PAGE_ELEMENT", pageElementRes.data);
  //       resolve(accessedRoutes);
  //     } else {
  //       let testjk = [];
  //       // 新增一个最后的404路由
  //       let router404 = {
  //         path: "*",
  //         redirect: "/404",
  //         hidden: true
  //       };
  //       // mymixin
  //       testjk.push(...asyncRoutes);
  //       testjk.push(router404);
  //       accessedRoutes = filterAsyncRouter(testjk);
  //       // let pageElementRes = await userService.queryPageElement(virtualSystemNo);
  //       // commit("SET_PAGE_ELEMENT", pageElementRes.data);
  //       commit("SET_ROUTES", accessedRoutes);
  //       resolve(accessedRoutes);
  //     }
  //   });
  // }
};

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  nowmenutype,
  actions
};
